import numpy as np
from PySide6 import QtCore, QtWidgets, QtGui
from ui.Ui_get_food_intake import Ui_GetIntake
# 建立一个qt窗口，用以接受用户输入
from module.kcal_algorithm import kcal_from_weight,compare_kcal_consumed,daily_nutrient_intake
from qt_material import apply_stylesheet

class MainWindow(QtWidgets.QMainWindow,Ui_GetIntake):
    def __init__(self):
        """
        初始化窗口
        """
        super().__init__()
        self.setupUi(self)
        self.setWindowTitle("饮食摄入")

        # 建立槽函数
        self.bind_slot()

    def bind_slot(self):
        """
        绑定槽函数
        """
        # 发送食物摄入数据按钮对应槽函数
        self.send_intake_food.clicked.connect(self.calculate_food)
        self.surplus_btn.clicked.connect(self.comapre_kcal_consumed)

    def comapre_kcal_consumed(self):
        # 计算热量对比
        total_kcal, total_carbohydrate_gram, total_protein_gram, total_fat_gram = compare_kcal_consumed()
        # 获取一天的目标热量和营养素
        kcal_target, fat_target, protein_target, carbohydrate_target = daily_nutrient_intake()
        # 计算盈余热量、营养素
        surplus = fat_target - total_fat_gram
        surplus_protein = protein_target - total_protein_gram
        surplus_fat = fat_target - total_fat_gram
        surplus_carbohydrate = carbohydrate_target - total_carbohydrate_gram
        # 弹出一个提示框
        QtWidgets.QMessageBox.information(self, "提示", f"热量已达：{np.round(total_kcal/kcal_target*100,2)}%,剩余补充热量：{np.round(surplus,2)}kcal"
                                                        f"脂肪含量已达：{np.round(total_fat_gram/fat_target*100,2)}%,剩余可进食脂肪含量为{np.round(surplus_fat,2)}克,"
                                                        f"蛋白质含量已达：{np.round(total_protein_gram/protein_target*100,2)}%,剩余可进食蛋白质含量为{np.round(surplus_protein,2)}克,"
                                                        f"碳水化合物含量已达：{np.round(total_carbohydrate_gram/carbohydrate_target*100,2)}%,剩余可进食碳水化合物含量为{np.round(surplus_carbohydrate,2)}克.")


    def calculate_food(self):
        """
        计算食物的热量、营养成分并发送到飞书
        :return:
        """
        # 获取数据
        food_name = self.intake_food_name.text()
        food_gram = self.intake_food_gram.text()
        # 计算营养成分和热量并同步发送到飞书
        kcal, fat_gram, protein_gram, carbohydrate_gram = kcal_from_weight(food_name=str(food_name),weight=float(food_gram))
        if kcal:
            # 弹出一个提示框
            QtWidgets.QMessageBox.information(self, "提示", f"本次摄入的食物为{food_name}，热量为{kcal}千卡，总共克重为{food_gram}克，脂肪含量为{fat_gram}克，蛋白质含量为{protein_gram}克，碳水化合物含量为{carbohydrate_gram}克。"
                                                            f"数据已发送到飞书多维表格。")
        self.comapre_kcal_consumed()





if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    apply_stylesheet(app, theme='light_blue.xml')
    window = MainWindow()
    window.show()
    app.exec()
